package booking;

import com.sun.mail.handlers.text_html;
import utility.PropertiesLoader;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class EmailClient implements EmailSender {

    @Override
    public void sendEmail(String content) {
        System.out.println("sending the email");
        Properties props = PropertiesLoader.getProperties();
        Session session = Session.getDefaultInstance(props,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(
                                PropertiesLoader.getPropertyValue("mail.username"),
                                PropertiesLoader.getPropertyValue("mail.password"));
                    }
                });

        try {
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("from@no-spam.com"));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse("07009003@hope.ac.uk"));
            message.setSubject("Testing Subject");
            message.setContent(content, "text/html");
            Transport.send(message);
            System.out.println("Done");
        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }
}
